package com.sfdk.admin.data.weaponEditor
{
    import com.sfdk.admin.data.RectangleEx;

    import flash.display.BitmapData;
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.geom.Matrix;
    import flash.geom.Rectangle;

    public class WeaponPiece extends EventDispatcher
    {
        public var id:int;

        public var direction:String;

        public var sourceRect : RectangleEx;

        private var _bitmapData:BitmapData;

        public function get bitmapData():BitmapData {
            return _bitmapData;
        }

        [Bindable]
        public function set bitmapData(value:BitmapData):void {
            var middleValue:BitmapData = value;
            if (_horizontalFlip && !_justHFlipped && !_verticalFlip) {
                var reverseMatrix:Matrix = new Matrix();
                reverseMatrix.scale(-1, 1);
                reverseMatrix.translate(middleValue.width, 0);
                var newBitmapData:BitmapData = new BitmapData(middleValue.width, middleValue.height, true, 0);
                newBitmapData.draw(middleValue, reverseMatrix);
                middleValue = newBitmapData;
            }
            if (_verticalFlip && !_justVFlipped && !_horizontalFlip) {
                reverseMatrix = new Matrix();
                reverseMatrix.scale(1, -1);
                reverseMatrix.translate(0, middleValue.height);
                newBitmapData = new BitmapData(middleValue.width, middleValue.height, true, 0);
                newBitmapData.draw(middleValue, reverseMatrix);
                middleValue = newBitmapData;
            }
             if (_verticalFlip && !_justVFlipped && _horizontalFlip && !_justHFlipped) {
                reverseMatrix = new Matrix();
                reverseMatrix.scale(-1, -1);
                reverseMatrix.translate(middleValue.width, bitmapData.height);
                newBitmapData = new BitmapData(middleValue.width, middleValue.height, true, 0);
                newBitmapData.draw(middleValue, reverseMatrix);
                middleValue = newBitmapData;
            }

            _justHFlipped = false;
            _justVFlipped = false;
            _bitmapData = middleValue;

            dispatchEvent(new Event(Event.CHANGE));
        }


        private var _horizontalFlip:Boolean;
        private var _justHFlipped:Boolean;

        public function get horizontalFlip():Boolean {
            return _horizontalFlip;
        }

        [Bindable]
        public function set horizontalFlip(value:Boolean):void {
            _horizontalFlip = value;
            _justHFlipped = true;

            var reverseMatrix:Matrix = new Matrix();
            if(_verticalFlip)
            {
                reverseMatrix.scale(-1, -1);
                reverseMatrix.translate(bitmapData.width, bitmapData.height);
            }
            else
            {
                reverseMatrix.scale(-1, 1);
                reverseMatrix.translate(bitmapData.width, 0);
            }

            var newBitmapData:BitmapData = new BitmapData(bitmapData.width, bitmapData.height, true, 0);
            newBitmapData.draw(bitmapData, reverseMatrix);
            bitmapData = newBitmapData;
        }


        private var _verticalFlip:Boolean;
        private var _justVFlipped:Boolean;

        public function get verticalFlip():Boolean {
            return _verticalFlip;
        }

        [Bindable]
        public function set verticalFlip(value:Boolean):void {
            _verticalFlip = value;
            _justVFlipped = true;

            var reverseMatrix:Matrix = new Matrix();
            if(_horizontalFlip)
            {
                reverseMatrix.scale(-1, -1);
                reverseMatrix.translate(bitmapData.width, bitmapData.height);
            }
            else
            {
                reverseMatrix.scale(1, -1);
                reverseMatrix.translate(0, bitmapData.height);
            }

            var newBitmapData:BitmapData = new BitmapData(bitmapData.width, bitmapData.height, true, 0);
            newBitmapData.draw(bitmapData, reverseMatrix);
            bitmapData = newBitmapData;
        }

    }


}